package com.lianxian.xindong.life.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.lianxian.xindong.life.R;
import com.lianxian.xindong.life.R2;
import com.lianxian.xindong.life.adapter.BusinessPhotoAdapter;
import com.lianxian.xindong.life.adapter.ComboAdapter;
import com.lianxian.xindong.life.entities.ComboModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BusinessDetailActivity extends AppCompatActivity {

    private TabLayout goodsDetailsTabLayout;
    private NestedScrollView mainScrollView;
    private View zhanwei_view;
    private View oneStartView;
    private View twoStartView;
    private View threeStartView;
    List<String> picUrls;
    List<ComboModel> comboModelList;
    BusinessPhotoAdapter photoAdapter;
    ComboAdapter comboAdapter;
    @BindView(R2.id.business_detail_head_recyclerview)
    RecyclerView businessPhotoRV;
    @BindView(R2.id.combo_recyclerview)
    RecyclerView comboRV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_detail);
        ButterKnife.bind(this);
        initData();
        initAdapters();
        initView();
    }

    private void initView() {
        goodsDetailsTabLayout = findViewById(R.id.goodsDetailsTabLayout);
        mainScrollView = findViewById(R.id.mainScrollView);
        oneStartView = findViewById(R.id.oneStartView);
        twoStartView = findViewById(R.id.twoStartView);
        threeStartView = findViewById(R.id.threeStartView);

        //1.点击Tab的 --> scroll定位
        goodsDetailsTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                int tabHeight = goodsDetailsTabLayout.getHeight();
                switch (position) {
                    case 0:
                        mainScrollView.smoothScrollTo(0, oneStartView.getTop());
                        break;
                    case 1:
                        mainScrollView.smoothScrollTo(0, (int) (twoStartView.getTop() ));
                        break;
                    case 2:
                        mainScrollView.smoothScrollTo(0, (int) (threeStartView.getTop() ));
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        //2.上下滑动scroll时的 --> tab定位
        mainScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//                if (scrollY < zhanwei_view.getHeight()) {
////                    goodsDetailsTabLayout.setVisibility(View.INVISIBLE);
//                } else {
                    int tabHeight = goodsDetailsTabLayout.getHeight();
                    goodsDetailsTabLayout.setVisibility(View.VISIBLE);
                    if (scrollY < oneStartView.getBottom()) {
                        goodsDetailsTabLayout.setVisibility(View.VISIBLE);
                        goodsDetailsTabLayout.setScrollPosition(0, 0f, true);
                    } else if (scrollY < twoStartView.getBottom()) {
                        goodsDetailsTabLayout.setVisibility(View.VISIBLE);
                        goodsDetailsTabLayout.setScrollPosition(1, 0f, true);
                    } else {
                        goodsDetailsTabLayout.setVisibility(View.VISIBLE);
                        goodsDetailsTabLayout.setScrollPosition(2, 0f, true);
                    }
//                }
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        businessPhotoRV.setLayoutManager(linearLayoutManager);
        businessPhotoRV.setAdapter(photoAdapter);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this);
        linearLayoutManager2.setOrientation(RecyclerView.VERTICAL);
        comboRV.setLayoutManager(linearLayoutManager2);
        comboRV.setAdapter(comboAdapter);
    }
    private void initAdapters(){
        photoAdapter = new BusinessPhotoAdapter(this,picUrls);
        comboAdapter = new ComboAdapter(comboModelList,this);
    }
    private void initData(){
        picUrls = new ArrayList<>();
        for (int i=0;i<10;i++){
            picUrls.add("https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&fmt=auto&app=138&f=JPEG?w=632&h=500");
        }
        comboModelList = new ArrayList<>();
        for (int i=0;i<5;i++){
            ComboModel comboModel = new ComboModel("https://img0.baidu.com/it/u=2202793414,1501173526&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500","绿茶茶冻+奶盖"
            ,"周一到周日",false,false,195,4.5f,1,"00:23:32");
            comboModelList.add(comboModel);
        }
    }
}